package com.climber.android.commonres.widget.loading

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.climber.android.commonres.R
import com.climber.android.commonres.helper.onClick

/**
 * Created by Mikes at 2020-05-21 11:49
 */
class CommonBigRetryView : LinearLayout {
    private lateinit var retryContent: TextView
    private lateinit var retryButton: ImageView

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        View.inflate(context, R.layout.com_view_big_retry, this)

        retryContent = findViewById<View>(R.id.retryContent) as TextView
        retryButton = findViewById<View>(R.id.retryButton) as ImageView

        updateData()
    }

    private fun updateData() {
        configure("")
    }

    fun setRetryListener(listener: (() -> Unit)? = null){
        retryButton.onClick {
            listener?.invoke()
        }
    }

    fun configure(retryContent: String?=null) {
        if (!TextUtils.isEmpty(retryContent)) {
            this.retryContent.text = retryContent
        }
    }
}